package org.zoomdev.zoom;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

// ==================== 核心API定义 ====================
public class Rule<T, C> {
    private final T data;
    private final C context;
    private final ArrayList<ProcessingStep<T, C>> steps = new ArrayList<>();

    private Rule(T data, C context) {
        this.data = data;
        this.context = context;
    }

    public static <T, C> Rule<T, C> of(T data, C context) {
        return new Rule<>(data, context);
    }

    public static void main(String[] args) {
        // 测试数据
        MyData data = new MyData("1001", "张三", "1345");
        MyContext context = new MyContext("RULE_001");

        // 执行规则链
        Rule.of(data, context)
                .append(ctx -> {
                    MyData d = ctx.data;
                    d.time = parseTime(d.ssnTime);
                })
                .filter(ctx -> ctx.data.time > 1200)
                .map(ctx -> {
                    MyData d = ctx.data;
                    return new HashMap<String, Object>() {{
                        put("id", d.id);
                        put("name", d.name);
                        put("ssnTime", d.ssnTime);
                        put("ruleId", ctx.context.id);
                    }};
                })
                .action(ctx -> saveToDatabase(ctx.data))
                .process();
    }

    public Rule<T, C> append(Consumer<DataContext<T, C>> action) {
        steps.add(new AppendStep<>(action));
        return this;
    }

    public Rule<T, C> filter(Predicate<DataContext<T, C>> predicate) {
        steps.add(new FilterStep<>(predicate));
        return this;
    }

    public <R> Rule<R, C> map(Function<DataContext<T, C>, R> mapper) {
        steps.add(new MapStep<>(mapper));
        return (Rule<R, C>) this;
    }

    public Rule<T, C> action(Consumer<DataContext<T, C>> action) {
        steps.add(new ActionStep<>(action));
        return this;
    }

    public void process() {
        DataContext<T, C> wrapper = new DataContext<>(data, context);
        for (ProcessingStep<T, C> step : steps) {
            if (!step.execute(wrapper)) {
                break; // 中断处理流程
            }
        }
    }

    // ==================== 工具方法 ====================
    private static int parseTime(String ssnTime) {
        return Integer.parseInt(ssnTime);
    }

    private static void saveToDatabase(Object data) {
        System.out.println("保存数据到数据库: " + data);
        // 实际数据库操作...
    }

    // ==================== 辅助类和接口 ====================
    private interface ProcessingStep<T, C> {
        boolean execute(DataContext<T, C> context);
    }

    private static class DataContext<T, C> {
        final C context;
        T data;

        DataContext(T data, C context) {
            this.data = data;
            this.context = context;
        }
    }

    private static class AppendStep<T, C> implements ProcessingStep<T, C> {
        private final Consumer<DataContext<T, C>> action;

        AppendStep(Consumer<DataContext<T, C>> action) {
            this.action = action;
        }

        @Override
        public boolean execute(DataContext<T, C> context) {
            action.accept(context);
            return true; // 继续执行
        }
    }

    private static class FilterStep<T, C> implements ProcessingStep<T, C> {
        private final Predicate<DataContext<T, C>> predicate;

        FilterStep(Predicate<DataContext<T, C>> predicate) {
            this.predicate = predicate;
        }

        @Override
        public boolean execute(DataContext<T, C> context) {
            return predicate.test(context); // 返回false则中断流程
        }
    }

    private static class MapStep<T, C, R> implements ProcessingStep<T, C> {
        private final Function<DataContext<T, C>, R> mapper;

        MapStep(Function<DataContext<T, C>, R> mapper) {
            this.mapper = mapper;
        }

        @Override
        public boolean execute(DataContext<T, C> context) {
            R newData = mapper.apply(context);
            context.data = (T) newData; // 类型转换需谨慎
            return true;
        }
    }

    private static class ActionStep<T, C> implements ProcessingStep<T, C> {
        private final Consumer<DataContext<T, C>> action;

        ActionStep(Consumer<DataContext<T, C>> action) {
            this.action = action;
        }

        @Override
        public boolean execute(DataContext<T, C> context) {
            action.accept(context);
            return true;
        }
    }

    // ==================== 使用示例 ====================
    static class MyData {
        String id;
        String name;
        String ssnTime;
        int time;

        MyData(String id, String name, String ssnTime) {
            this.id = id;
            this.name = name;
            this.ssnTime = ssnTime;
        }
    }

    static class MyContext {
        String id;

        MyContext(String id) {
            this.id = id;
        }
    }
}
